package bob.project.slick;


import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bob.project.ResourceManager;
import bob.project.object.GameObject;
import bob.project.object.Player;
import bob.project.object.StaticObject;

public class GameState extends BasicGameState implements ComponentListener {

	public static GameState instance;
	public ArrayList<GameObject> objects = new ArrayList<GameObject>();
	MouseOverArea weapon_hands;
	MouseOverArea weapon_shotgun;
	MouseOverArea weapon_smg;
	public Player player;
	private Image hud;
	private Image life;
	private Image energy;
	private Image hp;
	public boolean wasd[] = new boolean[4];
	
	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException
	{
		ResourceManager.getInstance().loadResources(ResourceManager.class.getResourceAsStream("resources.xml"));
		Color onOver = new Color(Color.lightGray.getRed(),Color.lightGray.getGreen(),Color.lightGray.getBlue(),0.4f);
		Color onDown = new Color(Color.white.getRed(),Color.white.getGreen(),Color.white.getBlue(),0.4f);
		weapon_hands = new MouseOverArea(arg0, ResourceManager.getInstance().getImage("weapon0"), 320, 32, this);
		weapon_hands.setMouseOverColor(onOver);
		weapon_hands.setMouseDownColor(onDown);
		weapon_shotgun = new MouseOverArea(arg0, ResourceManager.getInstance().getImage("weapon1"), 370, 32, this);
		weapon_shotgun.setMouseOverColor(onOver);
		weapon_shotgun.setMouseDownColor(onDown);
		weapon_smg = new MouseOverArea(arg0, ResourceManager.getInstance().getImage("weapon2"), 420, 32, this);
		weapon_smg.setMouseOverColor(onOver);
		weapon_smg.setMouseDownColor(onDown);
		
		hud = ResourceManager.getInstance().getImage("hud");
		life = ResourceManager.getInstance().getImage("life");
		Image layer = ResourceManager.getInstance().getImage("layer1");
		hp = layer.getSubImage(0, 0, 7, 12);
		energy = layer.getSubImage(0, 12, 7, 12);
		player = new Player();
		
		StaticObject obj = new StaticObject();
		obj.setDimensions(0, 400, 800, 200);
		objects.add(obj);
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
			throws SlickException
	{
		hud.draw(32, 32);
		if (player != null)
		{
			for(int i = 0; i < player.lives;i++)
				life.draw(32+132+i*16, 38);
			
			hp.draw(32+132, 55, player.hp*0.01f*40, 12);
			
			energy.draw(32+132, 55+12, player.energy*0.01f*40, 12);
		}
		weapon_hands.render(arg0, g);
		weapon_shotgun.render(arg0, g);
		weapon_smg.render(arg0, g);
		for(GameObject obj : objects)
			obj.draw(g);
		player.draw(g);
	}

	@Override
	public void update(GameContainer arg0, StateBasedGame arg1, int delta)
			throws SlickException
	{
		for(GameObject obj : objects)
			obj.update(delta);
		player.update(delta);

	}
	
	@Override
	public int getID()
	{
		return 0;
	}
	
	public void keyPressed(int key, char c)
	{
		if (key == Input.KEY_W)	
			wasd[0] = true;
		if (key == Input.KEY_A)	
			wasd[1] = true;
		if (key == Input.KEY_S)	
			wasd[2] = true;
		if (key == Input.KEY_D)	
			wasd[3] = true;
	}

	public void keyReleased(int key, char c)
	{
		if (key == Input.KEY_W)	
			wasd[0] = false;
		if (key == Input.KEY_A)	
			wasd[1] = false;
		if (key == Input.KEY_S)	
			wasd[2] = false;
		if (key == Input.KEY_D)	
			wasd[3] = false;
	}
	
	@Override
	public void componentActivated(AbstractComponent arg0) {
		// TODO Auto-generated method stub
		
	}

}
